package com.hxd.otc.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.widget.BlocksToast;
import com.hxd.otc.widget.LimitNumberTextWatcher;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lzy on 2018/6/9 0009.
 * description ${
 * <p>
 * Changed by somebody on 2018/6/9 0009
 * description ${TODO}
 */

public class PartConfirmPaymentDialog extends Dialog {
    @BindView(R.id.et_money)
    EditText et_money;
    @BindView(R.id.tv_totle_money)
    TextView tvTotleMoney;
    @BindView(R.id.tv_money_unit)
    TextView tvUnit;

    private OnConfirmClickListener onConfirmClickListener;


    public PartConfirmPaymentDialog(Context context) {
        this(context, R.style.BlockDialog);

    }


    private PartConfirmPaymentDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_part_confirm_payment);
        ButterKnife.bind(this);
        initWindows();
        et_money.addTextChangedListener(new LimitNumberTextWatcher(et_money, 8, 2));
    }

    private void initWindows() {
        setCanceledOnTouchOutside(true);
        Window window = getWindow();
        if (window != null) {
            DisplayMetrics dm = new DisplayMetrics();
            window.getWindowManager().getDefaultDisplay().getMetrics(dm);
            WindowManager.LayoutParams windowAttributes = window.getAttributes();
            windowAttributes.width = WindowManager.LayoutParams.MATCH_PARENT;
            windowAttributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
            windowAttributes.gravity = Gravity.CENTER;
            window.setAttributes(windowAttributes);
        }
    }

    @OnClick({R.id.tv_all, R.id.tv_cancel, R.id.tv_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_all:
                et_money.setText(tvTotleMoney.getText().toString());
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_confirm:
                onConfirm();
                break;
        }
    }

    private void onConfirm() {

        if (TextUtils.isEmpty(et_money.getText().toString())) {
            BlocksToast.get().showToast(getContext().getString(R.string.please_enter_the_amount_of_payment));
            return;
        }
        if (Double.parseDouble(et_money.getText().toString()) < 0) {
            BlocksToast.get().showToast(getContext().getString(R.string.amount_received_cannot_be_less_than_zero));
            return;
        }
        if (Double.parseDouble(et_money.getText().toString()) > Double.parseDouble(tvTotleMoney.getText().toString())) {
            BlocksToast.get().showToast(getContext().getString(R.string.cannot_greater_amount_uncollected_amount));
            return;
        }

        if (onConfirmClickListener == null) return;
        onConfirmClickListener.onConfirm(this, et_money.getText().toString());
    }


    /**
     * 设置确认回调监听
     */
    public PartConfirmPaymentDialog setOnConfirmClickListener(OnConfirmClickListener onConfirmClickListener) {
        this.onConfirmClickListener = onConfirmClickListener;
        return this;
    }

    /**
     * 设置确认回调监听
     */
    public PartConfirmPaymentDialog setContent(String unit, String money) {
        tvUnit.setText(unit);
        tvTotleMoney.setText(money);
        return this;
    }


    public interface OnConfirmClickListener {
        /**
         * 根据实现方式决定关闭对话框时机
         *
         * @param dialog 用于关闭对话框等操作
         * @param money  金额
         */
        void onConfirm(Dialog dialog, String money);
    }

}
